package com.tzy.user.controller;
import com.tzy.common.constant.Result;
import com.tzy.common.constant.ResultCode;
import com.tzy.common.controller.BaseController;
import com.tzy.common.entity.User;
import com.tzy.common.utils.EncryptUtil;
import com.tzy.common.utils.JsonWebTokenUtil;
import com.tzy.common.utils.JwtUtil;
import com.tzy.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.constructor.BaseConstructor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.ResultSet;
import java.util.List;

/**
 * 用户登录接口
 * https:blog.csdn.net/qq_37651267/article/details/99305364
 * @author 刘朋
 * <br/>date 2019-03-12
 */
//@Api(description = "登录接口")
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController extends BaseController {
    @Resource
    private UserService userService;
//    @ApiOperation(value = "登录")
    @PostMapping("/login/password")
    public Result login(String username, String password) {

        log.info("username={},password={}",username,password);
        if(StringUtils.isAnyBlank(username , password)){
            return Result.failure( ResultCode.FAILURE );//ResultCode.PARAM_NOT_COMPLETE,"参数错误"
        }
        User user = userService.loginByPassword(username, password);
        if( null != user ){
//            String token = JsonWebTokenUtil.getToken(user.getId());
            return Result.success(user);
        }
//        //生成一个token
//        String token = JwtUtil.createToken(username);
//        return Result.success(token);
        return Result.failure( ResultCode.USER_LOGIN_ERROR );
    }
    @GetMapping("/info/userId")
    public Result getInfo(HttpServletRequest request) {
        int userId = getUserId(request);
        log.info("getInfo() userId=" + userId);
        User user = userService.getUserInfoById(userId);
        if (user != null) {
            //用户头像为空则设置默认头像
            if (user.getProfilePhotoUrl() == null){
//                user.setProfilePhotoUrl(defaultProfilePhoto);
            }
//            user.getShippingAddress().split()
            return Result.success(user);
        }
        return Result.failure(ResultCode.FAILURE,"用户不存在");
    }/**
     * 修改密码
     *  PUT修改请求
     * @return
     */
    @PutMapping("/update")
    public Result updateUser(@RequestBody User user) {
        log.info("user="+user);
//        User user = new User();
//        user.setId(id);
//        user.setNickname(nickname);
        if( null != user ){
            if( !StringUtils.isBlank(user.getPassword()) ){
                log.info("密码不为空,user.password={}",user.getPassword());
                user.setPassword(EncryptUtil.getMd5(user.getPassword()));
            }
        }
        boolean updateById = userService.updateById(user);
        return updateById ? Result.success(true) : Result.failure(ResultCode.PARAM_NOT_COMPLETE);
    }
    @DeleteMapping("/delete")
    public Result delete(HttpServletRequest request) {
        int userId = getUserId(request);
        User user = new User();
        user.setId(userId);
        user.setStatus(1);
        boolean updateById = userService.updateById(user);
        if(updateById){
            return Result.success(updateById);
        }else{
            return Result.failure(ResultCode.REQUEST_ERROR);
        }

    }
    @GetMapping("/list")
    public Result list() {
        List<User> list = userService.list();
        return Result.success(list);
    }

}
